﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyOAuth.Core.Encrypt
{
    /// <summary>
    /// 密码生成/验证
    /// </summary>
    public class PasswordGenerator
    {
        /// <summary>
        /// 生成密码
        /// </summary>
        /// <param name="pass">密码明文</param>
        /// <param name="salt">密码盐值</param>
        /// <returns></returns>
        public static (string pass, string salt) Create(string pass, string salt = "")
        {
            if (string.IsNullOrWhiteSpace(salt))
            {
                salt = Guid.NewGuid().ToString("n");
            }
            var txt = $"{pass}{salt}";
            var ciphertext = MD5Encrypt.Encrypt(txt);
            return (ciphertext, salt);
        }

        /// <summary>
        /// 验证密码是否正确
        /// </summary>
        /// <param name="beforePass">加密前的明文</param>
        /// <param name="pass">加密后的密码</param>
        /// <param name="salt">加密盐值</param>
        /// <returns></returns>
        public static bool Validate(string beforePass, string pass, string salt)
        {
            var txt = $"{beforePass}{salt}";
            var ciphertext = MD5Encrypt.Encrypt(txt);
            if (ciphertext == pass)
            {
                return true;
            }
            return false;
        }

    }
}
